 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=149 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Splash screen
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='launch,Launcher,SplashScreen,splash,swing,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Splash screen</div>

<div class='main-body'>
 
<br>Splash screens are simple graphics which load quickly upon startup, and assure the user that 
the application is loading promptly.
The <tt><a href="http://java.sun.com/javase/6/docs/api/java/awt/SplashScreen.html">SplashScreen</a></tt> class
was added to JSE 6, and should be used if available.

<P>If JSE 6 is not available, then the following technique is provided as an alternative.

<p>Various policies for controlling the appearance and disappearance of
the splash screen are possible. Here, the splash screen disappears when
a main window has finished loading.
<p>This example is also of interest since it demonstrates clearly that
the launch thread is unusual - it is distinct from the event dispatch thread,
where most of the work of a Swing application takes place :
<ul>
<li>
upon launch, the launch thread shows a splash screen, which activates the
event dispatch thread</li>

<li>
once the splash screen is showing (or, more precisely, is "realized"),
the launch thread acts as a "worker thread", and can only close the splash
screen indirectly, using <tt>EventQueue.invokeLater</tt> to properly inject
an event into the event dispatch thread</li>
</ul>
<br>
<PRE>

<span class='keyword'>package</span> hirondelle.stocks.main;

<span class='keyword'>import</span> java.awt.*;
<span class='keyword'>import</span> java.net.URL;
<span class='keyword'>import</span> java.awt.image.ImageObserver;

<span class='comment'>/**
* Present a simple graphic to the user upon launch of the application, to 
* provide a faster initial response than is possible with the main window.
* 
* &lt;P&gt;Adapted from an 
* &lt;a href=http://developer.java.sun.com/developer/qow/archive/24/index.html&gt;item&lt;/a&gt; 
* on Sun's Java Developer Connection.
*
* &lt;P&gt;This splash screen appears within about 2.5 seconds on a development 
* machine. The main screen takes about 6.0 seconds to load, so use of a splash 
* screen cuts down the initial display delay by about 55 percent. 
*/</span>
<span class='keyword'>final</span> <span class='keyword'>class</span> SplashScreen <span class='keyword'>extends</span> Frame {

  <span class='comment'>/**
  * Construct using an image for the splash screen.
  *  
  * @param aImageId must have content, and is used by  
  * {@link Class#getResource(java.lang.String)} to retrieve the splash screen image.
  */</span>
  SplashScreen(String aImageId) {
    <span class='comment'>/* 
    * Implementation Note
    * Args.checkForContent is not called here, in an attempt to minimize 
    * class loading.
    */</span>
    <span class='keyword'>if</span> ( aImageId == <span class='keyword'>null</span> || aImageId.trim().length() == <span class='literal'>0</span> ){
      <span class='keyword'>throw</span> <span class='keyword'>new</span> IllegalArgumentException(<span class='literal'>"Image Id does not have content."</span>);
    }
    fImageId = aImageId;
  }
   
  <span class='comment'>/**
  * Show the splash screen to the end user.
  *
  * &lt;P&gt;Once this method returns, the splash screen is realized, which means 
  * that almost all work on the splash screen should proceed through the event 
  * dispatch thread. In particular, any call to &lt;tt&gt;dispose&lt;/tt&gt; for the 
  * splash screen must be performed in the event dispatch thread.
  */</span>
  <span class='keyword'>void</span> splash(){
    initImageAndTracker();
    setSize(fImage.getWidth(NO_OBSERVER), fImage.getHeight(NO_OBSERVER));
    center();
    
    fMediaTracker.addImage(fImage, IMAGE_ID);
    <span class='keyword'>try</span> {
      fMediaTracker.waitForID(IMAGE_ID);
    }
    <span class='keyword'>catch</span>(InterruptedException ex){
      System.out.println(<span class='literal'>"Cannot track image load."</span>);
    }

    SplashWindow splashWindow = <span class='keyword'>new</span> SplashWindow(<span class='keyword'>this</span>,fImage);
  }
  
  <span class='comment'>// PRIVATE//
</span>  <span class='keyword'>private</span> <span class='keyword'>final</span> String fImageId;
  <span class='keyword'>private</span> MediaTracker fMediaTracker;
  <span class='keyword'>private</span> Image fImage;
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> ImageObserver NO_OBSERVER = <span class='keyword'>null</span>; 
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> <span class='keyword'>int</span> IMAGE_ID = <span class='literal'>0</span>;

  <span class='keyword'>private</span> <span class='keyword'>void</span> initImageAndTracker(){
    fMediaTracker = <span class='keyword'>new</span> MediaTracker(<span class='keyword'>this</span>);
    URL imageURL = SplashScreen.<span class='keyword'>class</span>.getResource(fImageId);
    fImage = Toolkit.getDefaultToolkit().getImage(imageURL);
  }

  <span class='comment'>/**
  * Centers the frame on the screen.
  *
  *&lt;P&gt;This centering service is more or less in {@link hirondelle.stocks.util.ui.UiUtil}; 
  * this duplication is justified only because the use of  
  * {@link hirondelle.stocks.util.ui.UiUtil} would entail more class loading, which is 
  * not desirable for a splash screen.
  */</span>
  <span class='keyword'>private</span> <span class='keyword'>void</span> center(){
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle frame = getBounds();
    setLocation((screen.width - frame.width)/<span class='literal'>2</span>, (screen.height - frame.height)/<span class='literal'>2</span>);
  }
 
  <span class='keyword'>private</span> <span class='keyword'>class</span> SplashWindow <span class='keyword'>extends</span> Window {
    SplashWindow(Frame aParent, Image aImage) {
       <span class='keyword'>super</span>(aParent);
       fImage = aImage;
       setSize(fImage.getWidth(NO_OBSERVER), fImage.getHeight(NO_OBSERVER));
       Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
       Rectangle window = getBounds();
       setLocation((screen.width - window.width) / <span class='literal'>2</span>,(screen.height - window.height)/<span class='literal'>2</span>);
       setVisible(<span class='keyword'>true</span>);
    }
    <span class='keyword'>public</span> <span class='keyword'>void</span> paint(Graphics graphics) {
      <span class='keyword'>if</span> (fImage != <span class='keyword'>null</span>) {
        graphics.drawImage(fImage,<span class='literal'>0</span>,<span class='literal'>0</span>,<span class='keyword'>this</span>);
      }
    }
    <span class='keyword'>private</span> Image fImage;
  }
  
  <span class='comment'>/**
  * Developer test harness shows the splash screen for a fixed length of 
  * time, without launching the full application.
  */</span>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> main(String... aArgs){
    SplashScreen splashScreen = <span class='keyword'>new</span> SplashScreen(<span class='literal'>"StocksMonitor.gif"</span>);
    splashScreen.splash();
    <span class='keyword'>try</span> {
      Thread.sleep(<span class='literal'>2000</span>);
    }
    <span class='keyword'>catch</span>(InterruptedException ex) {
      System.out.println(ex);
    }
    System.exit(<span class='literal'>0</span>);
  }
}
 
</PRE>
<br>
<br>Here is an example of a class which launches an application using the
above <tt>SplashScreen</tt> :
<br>
<PRE>

<span class='keyword'>package</span> hirondelle.stocks.main;

<span class='keyword'>import</span> java.util.logging.*;
<span class='keyword'>import</span> java.awt.EventQueue;
<span class='keyword'>import</span> hirondelle.stocks.util.Util;
<span class='keyword'>import</span> hirondelle.stocks.util.Consts;

<span class='comment'>/**
* Launch the application.
*
*&lt;P&gt;Perform tasks in this order :
*&lt;ul&gt;
* &lt;li&gt;log basic system information 
* &lt;li&gt;promptly show a splash screen upon startup
* &lt;li&gt;show the main screen
* &lt;li&gt;remove the splash screen once the main screen is shown
*&lt;/ul&gt;
*
* These tasks are performed in a thread-safe manner.
*/</span>
<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> Launcher { 

  <span class='comment'>/**
  * Launch the application and display the main window.
  *
  * @param aArgs are ignored by this application, and may take any value.
  */</span>
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>void</span> main (String... aArgs) {
    
    <span class='comment'>/*
    * Implementation Note:
    *
    * Note that the launch thread of any GUI application is in effect an initial 
    * worker thread - it is not the event dispatch thread, where the bulk of processing
    * takes place. Thus, once the launch thread realizes a window, then the launch 
    * thread should almost always manipulate such a window through 
    * EventQueue.invokeLater. (This is done for closing the splash 
    * screen, for example.)
    */</span>
    
    <span class='comment'>//verifies that assertions are on:
</span>    <span class='comment'>//  assert(false) : "Test";
</span>    
    logBasicSystemInfo();
    showSplashScreen();
    showMainWindow();
    EventQueue.invokeLater( <span class='keyword'>new</span> SplashScreenCloser() );
    fLogger.info(<span class='literal'>"Launch thread now exiting..."</span>);
  }

  <span class='comment'>// PRIVATE //
</span>  
  <span class='keyword'>private</span> <span class='keyword'>static</span> SplashScreen fSplashScreen;
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> Logger fLogger = Util.getLogger(Launcher.<span class='keyword'>class</span>);
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> String SPLASH_IMAGE = <span class='literal'>"StocksMonitor.gif"</span>;

  <span class='comment'>/**
  * Show a simple graphical splash screen, as a quick preliminary to the main screen.
  */</span>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> showSplashScreen(){
    fLogger.info(<span class='literal'>"Showing the splash screen."</span>);
    fSplashScreen = <span class='keyword'>new</span> SplashScreen(SPLASH_IMAGE);
    fSplashScreen.splash();
  }
  
  <span class='comment'>/**
  * Display the main window of the application to the user.
  */</span>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> showMainWindow(){
    fLogger.info(<span class='literal'>"Showing the main window."</span>);
    StocksMonitorMainWindow mainWindow = <span class='keyword'>new</span> StocksMonitorMainWindow();
  }

  <span class='comment'>/** 
  * Removes the splash screen. 
  *
  * Invoke this &lt;tt&gt;Runnable&lt;/tt&gt; using 
  * &lt;tt&gt;EventQueue.invokeLater&lt;/tt&gt;, in order to remove the splash screen
  * in a thread-safe manner.
  */</span>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> <span class='keyword'>class</span> SplashScreenCloser <span class='keyword'>implements</span> Runnable {
    <span class='keyword'>public</span> <span class='keyword'>void</span> run(){
      fLogger.fine(<span class='literal'>"Closing the splash screen.'"</span>);
      fSplashScreen.dispose();
    }
  }
  
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> logBasicSystemInfo() {
    fLogger.info(<span class='literal'>"Launching the application..."</span>);
    fLogger.config(
      <span class='literal'>"Operating System: "</span> + System.getProperty(<span class='literal'>"os.name"</span>) + <span class='literal'>" "</span> + 
      System.getProperty(<span class='literal'>"os.version"</span>)
    );
    fLogger.config(<span class='literal'>"JRE: "</span> + System.getProperty(<span class='literal'>"java.version"</span>));
    fLogger.info(<span class='literal'>"Java Launched From: "</span> + System.getProperty(<span class='literal'>"java.home"</span>));
    fLogger.config(<span class='literal'>"Class Path: "</span> + System.getProperty(<span class='literal'>"java.class.path"</span>));
    fLogger.config(<span class='literal'>"Library Path: "</span> + System.getProperty(<span class='literal'>"java.library.path"</span>));
    fLogger.config(<span class='literal'>"Application Name: "</span> + Consts.APP_NAME + <span class='literal'>"/"</span> + Consts.APP_VERSION);
    fLogger.config(<span class='literal'>"User Home Directory: "</span> + System.getProperty(<span class='literal'>"user.home"</span>));
    fLogger.config(<span class='literal'>"User Working Directory: "</span> + System.getProperty(<span class='literal'>"user.dir"</span>));
    fLogger.info(<span class='literal'>"Test INFO logging."</span>);
    fLogger.fine(<span class='literal'>"Test FINE logging."</span>);
    fLogger.finest(<span class='literal'>"Test FINEST logging."</span>);
  }
} 
</PRE>
<br>
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction3abb-2.html?Id=153'>Swing threads</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='149'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=149 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
